<?php
namespace Admin\Controller;
use Think\Controller;
class activityController extends Controller

{
    public function index()
    {
        $this->display();
       
    }

    /*精彩活动信息*/
    public function activitylist(){
        $activity = M('activity');
        $activitylist = $activity->order("actid desc")->field('actid,actname,actpic,actintro,actaddress,acttime,actcost,actcall')->select();

        for($index=0;$index<count($activitylist);$index++)
        {
            $activitylist[$index]['number']=$index+1;
        }

        $this->assign('activitylist',$activitylist);
        $this->display();
    }
    
    /*精彩活动修改*/
public function editactivity()
{
        // $activity=M('activity');
        // $cid=$_GET['cid'];
        // $res = $activity->where("cid=$cid")->select();      
        // $this->activitylist=$res;//name属性对应的值 $this->对应值=$find;
        // $this->display();
        $actid=$_GET['actid'];
        $activityModel=M('activity');//表明,conf中定义前缀
        $activity = $activityModel->where("actid=$actid")->select();

        //dump($lost);
        $this->activity=$activity;//name属性对应的值 $this->对应值=$find;
        $this->display();
    }

    public function update(){

            $data=$_POST;
            $file=$_FILES;
            
            $actid=$data['actid'];
                   
            $activityModel = M('activity'); // 实例化User模型
            // 要修改的数据对象属性赋值
         
        if ($file['actpic']['size']==0) {
            //没有修改头像
            $result=$activityModel->where("actid=$actid")->save($data);
            
         
  
        }else{
            //修改头像
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize  = 3145728 ;// 设置附件上传大小
            $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =  './';
            $upload->savePath =  'Public/Uploads/photo/';// 设置附件上传目录
            $upload->autoSub = false;
            $upload->saveName = date("YmdHis").rand(100,999);//定义名字
            $info = $upload->upload();
    
            if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
             }else{// 上传成功 获取上传文件信息
                 //保存表单数据 包括附件数据
                


                $data['actpic']= $info['actpic']['savename']; 
                $result=$activityModel->where("actid=$actid")->save($data);
                    if ($data['oldpic']!='nopic.jpg') {
                    $oldpicPath='Public/Uploads/photo/'.$data['oldpic'];
                    unlink($oldpicPath);
                        }
                    }
            }
           
           if($result){
                $this->success('修改成功！','../activity/activitylist');
                }
               else{
                   $this->error('修改失败！');
                   }
    } 

    /*精彩活动添加*/
     public function addactivity()
    {
        $this->display();
       
    }
        public function upload(){
        $m = M("activity");
        $m->actname=I("post.actname");
        $m->actpic=I("post.actpic");
        $m->actintro=I("post.actintro");
        $m->actaddress=I("post.actaddress");
        $m->actime=I("actime");
        $m->actcost=I("actcost");
        $m->actcall=I("actcall");
        $m->clubid = $_SESSION['clubid'];
        if(false!=$m->add()){
            $this->redirect('activity/activitylist','',2,'添加成功，页面跳转中...');
        }else{
            $this->error("添加失败！");
        }
    }
   /*精彩活动删除*/
    public function deleteactivity()
    {
        $actid=I('actid');
        $actchModel = M('activity');
        $actch=$actchModel->where("actid=$actid")->find();
        // unlink(realpath("./Public/Uploads/logo/".$user["photo"]));
        if($actchModel->delete($vid)) {
            $this->success('删除成功');
        }
        else {
            $this->error('删除失败');
        }
    }
    
}
